//
//  NSString+textCheck.m
//  DAMI
//
//  Created by 陶泉 on 16/6/16.
//  Copyright © 2016年 广州大米网络科技有限公司. All rights reserved.
//

#import "NSString+textCheck.h"

@implementation NSString (textCheck)
//邮箱
- (BOOL)checkEmail
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}


//手机号码验证
- (BOOL) checkPhoneNum{
    
//    NSString *phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";//@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//    return [phoneTest evaluateWithObject:self];
    
//    NSString *phoneRegex = @"^1\\d{10}$";
    
    NSString *phoneRegex = @"^((13[0-9])|(14[57])|(15[0-35-9])|(16[6])|(17[0135-8])|(18[0-9])|(19[1289]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}


//验证码
- (BOOL) checkIndentityCode{
    
    //6位数字或者字母组成
    NSString *IDCode = @"^[0-9a-zA-Z]{6}$";
    NSPredicate *IDCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",IDCode];
    return [IDCodeTest evaluateWithObject:self];
    
}

//车牌号验证
- (BOOL) checkCarNum
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    //NSlog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:self];
}


//车型
- (BOOL) checkCarType
{
    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
    return [carTest evaluateWithObject:self];
}


//用户名
- (BOOL) checkUsername
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{4,15}$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:self];
    return B;
}


//密码 包含字母和数字的8-16位密码
- (BOOL) checkPassword
{
//    NSString *passWordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
    
    //大写 小写 数字 特殊字符
    NSString *passWordRegex = @"(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[_!@#$%^&*()-+.=<:>[~]+${}?]).{8,16}";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:self];
}


//昵称
- (BOOL) checkNickName
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:self];
}


//身份证号
- (BOOL) checkID
{
    BOOL flag;
    if (self.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:self];
}

//邮政编码
- (BOOL) checPostCode
{
    NSString *postCodeRegex = @"^[0-9]{6}$";
    NSPredicate *postCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",postCodeRegex];
    return [postCodePredicate evaluateWithObject:self];
}

/**
 *  价钱
 */
- (BOOL)checkPrice
{
    NSString *postCodeRegex = @"^[1-9]\\d*(\\.\\d*)?$";
    NSPredicate *postCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",postCodeRegex];
    return [postCodePredicate evaluateWithObject:self];
}

//检测中文，数字，字母
- (BOOL)checkChineseAndCharacterNum
{
    NSString *characterCodeRegex = @"^[\\u4e00-\\u9fa5|a-zA-Z|1-9]*$";
    NSPredicate *characterCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",characterCodeRegex];
    
    return [characterCodePredicate evaluateWithObject:self];
    
}
//0-9
- (BOOL)checkNumber
{
    NSString *characterCodeRegex = @"^[0-9]+$";
    NSPredicate *characterCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",characterCodeRegex];
    return [characterCodePredicate evaluateWithObject:self];
    
}


//判断是否有emoji
-(BOOL)checkContainsEmoji
{
    __block BOOL returnValue = NO;
    
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                             options:NSStringEnumerationByComposedCharacterSequences
                          usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                              const unichar high = [substring characterAtIndex: 0];
                              
                              // Surrogate pair (U+1D000-1F9FF)
                              if (0xD800 <= high && high <= 0xDBFF) {
                                  const unichar low = [substring characterAtIndex: 1];
                                  const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
                                  
                                  if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
                                      returnValue = YES;
                                  }
                                  
                                  // Not surrogate pair (U+2100-27BF)
                              } else {
                                  if (0x2100 <= high && high <= 0x27BF){
                                      returnValue = YES;
                                  }
                              }
                          }];
    
    return returnValue;
}


//判断是否全是空格
- (BOOL)checkBlankOrLinefeed
{
    return [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0;
}


//判断是否全是空格
- (BOOL)isEmpty{
    if (!self.length) {
        return true;
    } else {
        
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [self stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}



/** 纯汉字 */
- (BOOL)isValidChinese
{
    NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}


/**
 * 检查特殊字符
 */
- (BOOL)checkContainSpecialCharacters
{
    NSString *rangeRegex = @"[*?''\\\\/|:<>]"; // @"[:*?\\\"<>|,\\\\,\\/]";//[-()（）—”“$&@%^*?+?=|{}?【】？??￥!！.<>/:;：；、,，。]
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:rangeRegex options:0 error:nil];
    NSTextCheckingResult *match = [regular firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    if (match) {
        return YES;
    }
    return NO;
}


/** 英文 */
- (BOOL)checkContainLetter
{
    NSString *phoneRegex = @"[a-zA-Z]";
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:phoneRegex options:0 error:nil];
    NSTextCheckingResult *match = [regular firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    if (match) {
        return YES;
    }
    return NO;
}


/** 数字 */
- (BOOL)checkContainNumber
{
    NSString *numberRegex = @"[0-9]";
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:numberRegex options:0 error:nil];
    NSTextCheckingResult *match = [regular firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    if (match) {
        return YES;
    }
    return NO;
}



/**
 * 检查是否只有字母和数字
 */
- (BOOL)checkCharactersAndNumbersLenth:(NSInteger)lenth
{
    NSString *regex = [NSString stringWithFormat:@"^[a-zA-Z0-9]{%ld}$",lenth];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}




- (BOOL)isValidWithMinLenth:(NSInteger)minLenth
                   maxLenth:(NSInteger)maxLenth
             containChinese:(BOOL)containChinese
              containDigtal:(BOOL)containDigtal
              containLetter:(BOOL)containLetter
      containOtherCharacter:(NSString *)containOtherCharacter
        firstCannotBeDigtal:(BOOL)firstCannotBeDigtal
{
    NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
    NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @"";
    NSString *lengthRegex = [NSString stringWithFormat:@"(?=^.{%@,%@}$)", @(minLenth), @(maxLenth)];
    NSString *digtalRegex = containDigtal ? @"(?=(.*\\d.*){1})" : @"";
    NSString *letterRegex = containLetter ? @"(?=(.*[a-zA-Z].*){1})" : @"";
    NSString *characterRegex = [NSString stringWithFormat:@"(?:%@[%@A-Za-z0-9%@]+)", first, hanzi, containOtherCharacter ? containOtherCharacter : @""];
    NSString *regex = [NSString stringWithFormat:@"%@%@%@%@", lengthRegex, digtalRegex, letterRegex, characterRegex];
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:self];
}




/**
 * 检查不需要的字符
 */
- (BOOL)checkContainCharacters:(NSString *)characters
{
    //characters @"[*?''\\\\/|:<>]"
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:characters options:0 error:nil];
    NSTextCheckingResult *match = [regular firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    if (match) {
        return YES;
    }
    return NO;
}




/**
 * 筛选掉特殊字符
 */
- (NSString *)getNewStoreNameByRegularExpressionWithText:(NSString *)text{
    NSString *storeStr = [NSString stringWithFormat:@"%@",text];
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[*?''\\\\/|:<>]" options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators | NSRegularExpressionAnchorsMatchLines | NSRegularExpressionAllowCommentsAndWhitespace error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:storeStr options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
    return result;
}

/**
* 替换掉字符串中间连续的多个逗号
*/
- (NSString  *)string:(NSString *)string replaceStr:(NSString *)replaceStr
{
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"\\,{2,}" options:0 error:nil];
    string  = [regularExpression stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:replaceStr];
    return string;
}



@end

